﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit.Electric.Common;

namespace HYElectricSetting
{
    public partial class ConduitTypeForm : System.Windows.Forms.Form, IElectricSettingItem
	{
		public ConduitTypeForm(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			this.doc = doc;
			this.isEdited = false;
			this.isReady = false;
			this.lstFitting = new List<Element>();
			this.selectedItem = null;
		}

		private void ConduitTypeForm_Load(object sender, EventArgs e)
		{
			this.PrepareSettingData();
			this.GetConduitFittingFromDoc();
			this.InitConduitTypeCombox();
			this.isReady = true;
		}

		public bool IsEdited
		{
			get
			{
				return this.isEdited;
			}
			set
			{
				this.isEdited = value;
				HYElectricSettingMainForm hyelectricSettingMainForm = base.Tag as HYElectricSettingMainForm;
				bool flag = this.isReady && this.isEdited && hyelectricSettingMainForm != null;
				if (flag)
				{
					hyelectricSettingMainForm.ValidApplyButton();
				}
			}
		}

		public void UpdateFormData()
		{
			bool flag = this.isReady;
			if (flag)
			{
				this.InitConduitTypeCombox();
			}
		}

		public void SaveElectricSettingData()
		{
			ElectricSettingData.SaveConduitSettingData(this.doc);
		}

		private void PrepareSettingData()
		{
			ElectricSettingData.InitConduitSettingProcessData(this.doc);
		}

		private void GetConduitFittingFromDoc()
		{
			this.lstFitting.Clear();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.doc);
			this.lstFitting = filteredElementCollector.OfCategory(BuiltInCategory.OST_ConduitFitting).OfClass(typeof(FamilySymbol)).ToList<Element>();
		}

		private void InitConduitTypeCombox()
		{
			this.cmbConduitType.Items.Clear();
			foreach (ConduitTypeData item in ElectricSettingData.ConduitSettingProcessData)
			{
				this.cmbConduitType.Items.Add(item);
			}
			bool flag = this.selectedItem != null && this.cmbConduitType.Items.Contains(this.selectedItem);
			if (flag)
			{
				this.cmbConduitType.SelectedItem = this.selectedItem;
			}
			else
			{
				bool flag2 = this.cmbConduitType.Items.Count > 0;
				if (flag2)
				{
					this.cmbConduitType.SelectedIndex = 0;
				}
			}
		}

		private void UpdateElbowCombox()
		{
			this.cmbElbow.Items.Clear();
			foreach (Element element in this.lstFitting)
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				bool flag = familySymbol == null;
				if (!flag)
				{
					bool flag2 = familySymbol.Family.get_Parameter(BuiltInParameter.FAMILY_CONTENT_PART_TYPE).AsInteger() == 5;
					if (flag2)
					{
						string name = familySymbol.Family.Name + " : " + familySymbol.Name;
						this.cmbElbow.Items.Add(new ElementToName(name, element));
					}
				}
			}
			ConduitTypeData conduitTypeData = this.cmbConduitType.SelectedItem as ConduitTypeData;
			bool flag3 = conduitTypeData == null || conduitTypeData.Elbow == null;
			if (!flag3)
			{
				foreach (object obj in this.cmbElbow.Items)
				{
					ElementToName elementToName = (ElementToName)obj;
					bool flag4 = conduitTypeData.Elbow.Id == elementToName.element.Id;
					if (flag4)
					{
						this.cmbElbow.SelectedItem = elementToName;
						break;
					}
				}
			}
		}

		private void UpdateTeeCombox()
		{
			this.cmbTee.Items.Clear();
			foreach (Element element in this.lstFitting)
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				bool flag = familySymbol == null;
				if (!flag)
				{
					bool flag2 = familySymbol.Family.get_Parameter(BuiltInParameter.FAMILY_CONTENT_PART_TYPE).AsInteger() == 30;
					if (flag2)
					{
						string name = familySymbol.Family.Name + " : " + familySymbol.Name;
						this.cmbTee.Items.Add(new ElementToName(name, element));
					}
				}
			}
			ConduitTypeData conduitTypeData = this.cmbConduitType.SelectedItem as ConduitTypeData;
			bool flag3 = conduitTypeData == null || conduitTypeData.Tee == null;
			if (!flag3)
			{
				foreach (object obj in this.cmbTee.Items)
				{
					ElementToName elementToName = (ElementToName)obj;
					bool flag4 = conduitTypeData.Tee.Id == elementToName.element.Id;
					if (flag4)
					{
						this.cmbTee.SelectedItem = elementToName;
						break;
					}
				}
			}
		}

		private void UpdateCrossCombox()
		{
			this.cmbCross.Items.Clear();
			foreach (Element element in this.lstFitting)
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				bool flag = familySymbol == null;
				if (!flag)
				{
					bool flag2 = familySymbol.Family.get_Parameter(BuiltInParameter.FAMILY_CONTENT_PART_TYPE).AsInteger() == 31;
					if (flag2)
					{
						string name = familySymbol.Family.Name + " : " + familySymbol.Name;
						this.cmbCross.Items.Add(new ElementToName(name, element));
					}
				}
			}
			ConduitTypeData conduitTypeData = this.cmbConduitType.SelectedItem as ConduitTypeData;
			bool flag3 = conduitTypeData == null || conduitTypeData.Cross == null;
			if (!flag3)
			{
				foreach (object obj in this.cmbCross.Items)
				{
					ElementToName elementToName = (ElementToName)obj;
					bool flag4 = conduitTypeData.Cross.Id == elementToName.element.Id;
					if (flag4)
					{
						this.cmbCross.SelectedItem = elementToName;
						break;
					}
				}
			}
		}

		private void UpdateTransitionCombox()
		{
			this.cmbTransition.Items.Clear();
			foreach (Element element in this.lstFitting)
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				bool flag = familySymbol == null;
				if (!flag)
				{
					bool flag2 = familySymbol.Family.get_Parameter(BuiltInParameter.FAMILY_CONTENT_PART_TYPE).AsInteger() == 7;
					if (flag2)
					{
						string name = familySymbol.Family.Name + " : " + familySymbol.Name;
						this.cmbTransition.Items.Add(new ElementToName(name, element));
					}
				}
			}
			ConduitTypeData conduitTypeData = this.cmbConduitType.SelectedItem as ConduitTypeData;
			bool flag3 = conduitTypeData == null || conduitTypeData.Transition == null;
			if (!flag3)
			{
				foreach (object obj in this.cmbTransition.Items)
				{
					ElementToName elementToName = (ElementToName)obj;
					bool flag4 = conduitTypeData.Transition.Id == elementToName.element.Id;
					if (flag4)
					{
						this.cmbTransition.SelectedItem = elementToName;
						break;
					}
				}
			}
		}

		private void UpdateUnionCombox()
		{
			this.cmbUnion.Items.Clear();
			foreach (Element element in this.lstFitting)
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				bool flag = familySymbol == null;
				if (!flag)
				{
					bool flag2 = familySymbol.Family.get_Parameter(BuiltInParameter.FAMILY_CONTENT_PART_TYPE).AsInteger() == 13;
					if (flag2)
					{
						string name = familySymbol.Family.Name + " : " + familySymbol.Name;
						this.cmbUnion.Items.Add(new ElementToName(name, element));
					}
				}
			}
			ConduitTypeData conduitTypeData = this.cmbConduitType.SelectedItem as ConduitTypeData;
			bool flag3 = conduitTypeData == null || conduitTypeData.Union == null;
			if (!flag3)
			{
				foreach (object obj in this.cmbUnion.Items)
				{
					ElementToName elementToName = (ElementToName)obj;
					bool flag4 = conduitTypeData.Union.Id == elementToName.element.Id;
					if (flag4)
					{
						this.cmbUnion.SelectedItem = elementToName;
						break;
					}
				}
			}
		}

		private void cmbConduitType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.selectedItem = (this.cmbConduitType.SelectedItem as ConduitTypeData);
			this.UpdateElbowCombox();
			this.UpdateTeeCombox();
			this.UpdateCrossCombox();
			this.UpdateTransitionCombox();
			this.UpdateUnionCombox();
		}

		private void cmbElbow_SelectedIndexChanged(object sender, EventArgs e)
		{
			ConduitTypeData conduitTypeData = this.cmbConduitType.SelectedItem as ConduitTypeData;
			ElementToName elementToName = this.cmbElbow.SelectedItem as ElementToName;
			bool flag = conduitTypeData != null && elementToName != null;
			if (flag)
			{
				bool flag2 = conduitTypeData.Elbow == null && elementToName.element == null;
				if (!flag2)
				{
					bool flag3 = conduitTypeData.Elbow != null && elementToName.element != null && conduitTypeData.Elbow.Id == elementToName.element.Id;
					if (!flag3)
					{
						conduitTypeData.Elbow = (elementToName.element as FamilySymbol);
						this.IsEdited = true;
					}
				}
			}
		}

		private void cmbTee_SelectedIndexChanged(object sender, EventArgs e)
		{
			ConduitTypeData conduitTypeData = this.cmbConduitType.SelectedItem as ConduitTypeData;
			ElementToName elementToName = this.cmbTee.SelectedItem as ElementToName;
			bool flag = conduitTypeData != null && elementToName != null;
			if (flag)
			{
				bool flag2 = conduitTypeData.Tee == null && elementToName.element == null;
				if (!flag2)
				{
					bool flag3 = conduitTypeData.Tee != null && elementToName.element != null && conduitTypeData.Tee.Id == elementToName.element.Id;
					if (!flag3)
					{
						conduitTypeData.Tee = (elementToName.element as FamilySymbol);
						this.IsEdited = true;
					}
				}
			}
		}

		private void cmbCross_SelectedIndexChanged(object sender, EventArgs e)
		{
			ConduitTypeData conduitTypeData = this.cmbConduitType.SelectedItem as ConduitTypeData;
			ElementToName elementToName = this.cmbCross.SelectedItem as ElementToName;
			bool flag = conduitTypeData != null && elementToName != null;
			if (flag)
			{
				bool flag2 = conduitTypeData.Cross == null && elementToName.element == null;
				if (!flag2)
				{
					bool flag3 = conduitTypeData.Cross != null && elementToName.element != null && conduitTypeData.Cross.Id == elementToName.element.Id;
					if (!flag3)
					{
						conduitTypeData.Cross = (elementToName.element as FamilySymbol);
						this.IsEdited = true;
					}
				}
			}
		}

		private void cmbTransition_SelectedIndexChanged(object sender, EventArgs e)
		{
			ConduitTypeData conduitTypeData = this.cmbConduitType.SelectedItem as ConduitTypeData;
			ElementToName elementToName = this.cmbTransition.SelectedItem as ElementToName;
			bool flag = conduitTypeData != null && elementToName != null;
			if (flag)
			{
				bool flag2 = conduitTypeData.Transition == null && elementToName.element == null;
				if (!flag2)
				{
					bool flag3 = conduitTypeData.Transition != null && elementToName.element != null && conduitTypeData.Transition.Id == elementToName.element.Id;
					if (!flag3)
					{
						conduitTypeData.Transition = (elementToName.element as FamilySymbol);
						this.IsEdited = true;
					}
				}
			}
		}

		private void cmbUnion_SelectedIndexChanged(object sender, EventArgs e)
		{
			ConduitTypeData conduitTypeData = this.cmbConduitType.SelectedItem as ConduitTypeData;
			ElementToName elementToName = this.cmbUnion.SelectedItem as ElementToName;
			bool flag = conduitTypeData != null && elementToName != null;
			if (flag)
			{
				bool flag2 = conduitTypeData.Union == null && elementToName.element == null;
				if (!flag2)
				{
					bool flag3 = conduitTypeData.Union != null && elementToName.element != null && conduitTypeData.Union.Id == elementToName.element.Id;
					if (!flag3)
					{
						conduitTypeData.Union = (elementToName.element as FamilySymbol);
						this.IsEdited = true;
					}
				}
			}
		}

		private void buttonElbow_Click(object sender, EventArgs e)
		{
            this.LoadConduitFitting((PartType)5);
		}

		private void buttonTee_Click(object sender, EventArgs e)
		{
            this.LoadConduitFitting((PartType)30);
		}

		private void buttonCross_Click(object sender, EventArgs e)
		{
            this.LoadConduitFitting((PartType)31);
		}

		private void buttonTransition_Click(object sender, EventArgs e)
		{
            this.LoadConduitFitting((PartType)7);
		}

		private void buttonUnion_Click(object sender, EventArgs e)
		{
            this.LoadConduitFitting((PartType)13);
		}

		private void LoadConduitFitting(PartType type)
		{
			FamilyLibDataAssist familyLibDataAssist = new FamilyLibDataAssist(this.doc, "ConduitFitting");
			List<DbFamilyObject> objects = familyLibDataAssist.LoadFamilyDatas(new ConduitFittingFilter((int)type));
			FittingSelectForm fittingSelectForm = new FittingSelectForm("加载线管配件", this.doc, (Autodesk.Revit.DB.BuiltInCategory)(-2008128), objects);
			bool flag = fittingSelectForm.ShowDialog() != DialogResult.OK;
			if (!flag)
			{
				FamilySymbol familySymbol = fittingSelectForm.GetFamilySymbol();
				bool flag2 = familySymbol == null;
				if (!flag2)
				{
					this.GetConduitFittingFromDoc();
					this.UpdateElbowCombox();
					this.UpdateTeeCombox();
					this.UpdateCrossCombox();
					this.UpdateTransitionCombox();
					this.UpdateUnionCombox();
					System.Windows.Forms.ComboBox comboBox = null;
                    if ((int)type <= 7)
					{
                        if ((int)type != 5)
						{
                            if ((int)type == 7)
							{
								comboBox = this.cmbTransition;
							}
						}
						else
						{
							comboBox = this.cmbElbow;
						}
					}
                    else if ((int)type != 13)
					{
                        if ((int)type != 30)
						{
                            if ((int)type == 31)
							{
								comboBox = this.cmbCross;
							}
						}
						else
						{
							comboBox = this.cmbTee;
						}
					}
					else
					{
						comboBox = this.cmbUnion;
					}
					bool flag3 = comboBox == null;
					if (!flag3)
					{
						foreach (object obj in comboBox.Items)
						{
							ElementToName elementToName = (ElementToName)obj;
							bool flag4 = familySymbol.Id == elementToName.element.Id;
							if (flag4)
							{
								comboBox.SelectedItem = elementToName;
								break;
							}
						}
					}
				}
			}
		}

		private const string dbTableName = "ConduitFitting";

		private Document doc = null;

		private bool isEdited = false;

		private bool isReady = false;

		private List<Element> lstFitting;

		private ConduitTypeData selectedItem = null;
	}
}
